<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="复检单号" prop="code">
        <el-input v-model="formData.code" placeholder="请输入复检单号" />
      </el-form-item>
      <el-form-item label="图号id" prop="drawingNoId">
        <el-input v-model="formData.drawingNoId" placeholder="请输入图号id" />
      </el-form-item>
      <el-form-item label="模具id" prop="accountId">
        <el-input v-model="formData.accountId" placeholder="请输入模具id" />
      </el-form-item>
      <el-form-item label="接收人Id" prop="assigneerId">
        <el-input v-model="formData.assigneerId" placeholder="请输入接收人Id" />
      </el-form-item>
      <el-form-item label="复检人Ids" prop="assigneerIds">
        <el-input v-model="formData.assigneerIds" placeholder="请输入复检人Ids" />
      </el-form-item>
      <el-form-item label="复检人名字" prop="assigneerNames">
        <el-input v-model="formData.assigneerNames" placeholder="请输入复检人名字" />
      </el-form-item>
      <el-form-item label="复检类型id" prop="recheckId">
        <el-input v-model="formData.recheckId" placeholder="请输入复检类型id" />
      </el-form-item>
      <el-form-item label="紧急程度Id" prop="urgency">
        <el-input v-model="formData.urgency" placeholder="请输入紧急程度Id" />
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="计划复检日期" prop="nextRecheckTime">
        <el-date-picker
          v-model="formData.nextRecheckTime"
          type="date"
          value-format="x"
          placeholder="选择计划复检日期"
        />
      </el-form-item>
      <el-form-item label="实际复检日期" prop="realRecheckTime">
        <el-date-picker
          v-model="formData.realRecheckTime"
          type="date"
          value-format="x"
          placeholder="选择实际复检日期"
        />
      </el-form-item>
      <el-form-item label="任务下达时间" prop="taskCreateTime">
        <el-date-picker
          v-model="formData.taskCreateTime"
          type="date"
          value-format="x"
          placeholder="选择任务下达时间"
        />
      </el-form-item>
      <el-form-item label="任务接收时间" prop="taskReceiveTime">
        <el-date-picker
          v-model="formData.taskReceiveTime"
          type="date"
          value-format="x"
          placeholder="选择任务接收时间"
        />
      </el-form-item>
      <el-form-item label="作业开始时间" prop="jobStartTime">
        <el-date-picker
          v-model="formData.jobStartTime"
          type="date"
          value-format="x"
          placeholder="选择作业开始时间"
        />
      </el-form-item>
      <el-form-item label="任务开始时间" prop="taskStartTime">
        <el-date-picker
          v-model="formData.taskStartTime"
          type="date"
          value-format="x"
          placeholder="选择任务开始时间"
        />
      </el-form-item>
      <el-form-item label="任务结束时间" prop="taskEndTime">
        <el-date-picker
          v-model="formData.taskEndTime"
          type="date"
          value-format="x"
          placeholder="选择任务结束时间"
        />
      </el-form-item>
      <el-form-item label="任务执行时长" prop="taskExecutionDuration">
        <el-input v-model="formData.taskExecutionDuration" placeholder="请输入任务执行时长" />
      </el-form-item>
      <el-form-item label="实际开始时间" prop="actualStartTime">
        <el-date-picker
          v-model="formData.actualStartTime"
          type="date"
          value-format="x"
          placeholder="选择实际开始时间"
        />
      </el-form-item>
      <el-form-item label="实际结束时间" prop="actualEndTime">
        <el-date-picker
          v-model="formData.actualEndTime"
          type="date"
          value-format="x"
          placeholder="选择实际结束时间"
        />
      </el-form-item>
      <el-form-item label="实际持续时长" prop="actualExecutionDuration">
        <el-input v-model="formData.actualExecutionDuration" placeholder="请输入实际持续时长" />
      </el-form-item>
      <el-form-item label="复检完整" prop="isCompleted">
        <el-input v-model="formData.isCompleted" placeholder="请输入复检完整" />
      </el-form-item>
      <el-form-item label="审核说明" prop="auditDescription">
        <Editor v-model="formData.auditDescription" height="150px" />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <Editor v-model="formData.description" height="150px" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { RecheckTaskApi, RecheckTask } from '@/api/mold/rechecktask'

/** 复检任务 表单 */
defineOptions({ name: 'RecheckTaskForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  code: undefined,
  drawingNoId: undefined,
  accountId: undefined,
  assigneerId: undefined,
  assigneerIds: undefined,
  assigneerNames: undefined,
  recheckId: undefined,
  urgency: undefined,
  status: undefined,
  nextRecheckTime: undefined,
  realRecheckTime: undefined,
  taskCreateTime: undefined,
  taskReceiveTime: undefined,
  jobStartTime: undefined,
  taskStartTime: undefined,
  taskEndTime: undefined,
  taskExecutionDuration: undefined,
  actualStartTime: undefined,
  actualEndTime: undefined,
  actualExecutionDuration: undefined,
  isCompleted: undefined,
  auditDescription: undefined,
  description: undefined,
})
const formRules = reactive({
  code: [{ required: true, message: '复检单号不能为空', trigger: 'blur' }],
  drawingNoId: [{ required: true, message: '图号id不能为空', trigger: 'blur' }],
  accountId: [{ required: true, message: '模具id不能为空', trigger: 'blur' }],
  recheckId: [{ required: true, message: '复检类型id不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await RecheckTaskApi.getRecheckTask(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as RecheckTask
    if (formType.value === 'create') {
      await RecheckTaskApi.createRecheckTask(data)
      message.success(t('common.createSuccess'))
    } else {
      await RecheckTaskApi.updateRecheckTask(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    code: undefined,
    drawingNoId: undefined,
    accountId: undefined,
    assigneerId: undefined,
    assigneerIds: undefined,
    assigneerNames: undefined,
    recheckId: undefined,
    urgency: undefined,
    status: undefined,
    nextRecheckTime: undefined,
    realRecheckTime: undefined,
    taskCreateTime: undefined,
    taskReceiveTime: undefined,
    jobStartTime: undefined,
    taskStartTime: undefined,
    taskEndTime: undefined,
    taskExecutionDuration: undefined,
    actualStartTime: undefined,
    actualEndTime: undefined,
    actualExecutionDuration: undefined,
    isCompleted: undefined,
    auditDescription: undefined,
    description: undefined,
  }
  formRef.value?.resetFields()
}
</script>